package com.yusheng.tr.multi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yusheng.exceptions.ServiceException;
import com.yusheng.tr.common.core.entity.multi.TransDistEntity;;
import com.yusheng.tr.common.core.model.dto.multi.TransDistDTO;
import com.yusheng.tr.multi.mapper.TransDistMapper;
import com.yusheng.tr.multi.service.ITransDistService;
import com.yusheng.tr.common.core.model.vo.multi.DistinctAreaVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 运输距离表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-09-27
 */
@Service
public class TransDistServiceImpl extends ServiceImpl<TransDistMapper, TransDistEntity> implements ITransDistService {

    @Autowired
    private TransDistMapper transDistMapper;

    /**
     * 查询运输距离列表数据
     *
     * @param params
     * @return
     */
    @Override
    public List<TransDistEntity> queryTransDistList(TransDistEntity params) {
        /**
         * 查询 transDistMapper
         */
        List<TransDistEntity> distList = transDistMapper.queryTransDistList(params);
        /**
         * 返回
         */
        return distList;
    }

    @Override
    public IPage<TransDistEntity> queryPageTransDistList(Integer current, Integer size, String startArea,String endArea) {
        /**
         * 校验参数，解析参数
         */
        if (current <= 0) {
            throw new ServiceException("页码不存在");
        }
        if (size <= 0) {
            throw new ServiceException("页数不存在");
        }

        /**
         * 分页查询
         */
        Page<TransDistEntity> page = new Page<>(current, size);
        QueryWrapper<TransDistEntity> queryWrapper = new QueryWrapper<>();
        /**
         * 根据区块查询
         */
        if (StringUtils.isNotBlank(startArea)) {
            queryWrapper.lambda().like(TransDistEntity::getStartArea, startArea);
        }
        if(StringUtils.isNotBlank(endArea)){
            queryWrapper.lambda().like(TransDistEntity::getEndArea,endArea);
        }

        /**
         * 返回
         */
        IPage<TransDistEntity> resultPage =  transDistMapper.selectPage(page,queryWrapper);
        return resultPage;

    }

    /**
     * 新增运输接口
     */
    @Override
    @Transactional
    public void insert(TransDistDTO transDistDTO) {
        TransDistEntity distEntity = new TransDistEntity();
        BeanUtils.copyProperties(transDistDTO, distEntity);
         save(distEntity);
    }

    /**
     * 更新数据接口
     */
    @Override
    public void updateData(TransDistDTO transDistDTO) {
        TransDistEntity transDist = new TransDistEntity();
       transDist.setId(transDistDTO.getId());
        transDist.setDist(transDistDTO.getDist());
        transDist.setStartArea(transDistDTO.getStartArea());
        transDist.setEndArea(transDistDTO.getEndArea());
        transDistMapper.updateById(transDist);
    }

    /**
     * 对开始区域，到达区域，做去重数据列表查询
     */
    @Override
    public DistinctAreaVO distinctArea() {
        List<String> distinctStartArea = transDistMapper.selectDistinctStartArea();
        List<String> distinctEndArea = transDistMapper.selectDistinctEndArea();

        List<DistinctAreaVO> distinctAreas = new ArrayList<>();
        DistinctAreaVO distinctAreaVO = new DistinctAreaVO();
        distinctAreaVO.setDistinctStartArea(distinctStartArea);
        distinctAreaVO.setDistinctEndArea(distinctEndArea);
        distinctAreas.add(distinctAreaVO);
        return distinctAreaVO;
    }

    /**
     * 对始发地，目的地进行查询
     * @param start
     * @param end
     * @return
     */
    @Override
    public TransDistEntity queryArea(String start,String end){
        TransDistEntity entity = getOne(new LambdaQueryWrapper<TransDistEntity>().eq(TransDistEntity::getStartArea,start).eq(TransDistEntity::getEndArea,end));
        if (entity == null){
            entity = getOne(new LambdaQueryWrapper<TransDistEntity>().eq(TransDistEntity::getStartArea,end).eq(TransDistEntity::getEndArea,start));
        }
        return entity;
    }
}
